package com.qilu.pe;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.base.Config;
import com.qilu.pe.base.Global;
import com.qilu.pe.base.MyApp;
import com.qilu.pe.dao.bean.EventModel;
import com.qilu.pe.dao.bean.SettingsData;
import com.qilu.pe.dao.http.APIRetrofit;
import com.qilu.pe.support.util.Logg;
import com.qilu.pe.support.widget.BottomNavigationViewEx;
import com.qilu.pe.ui.fragment.Home1Fragment;
import com.qilu.pe.ui.fragment.Home2Fragment;
import com.qilu.pe.ui.fragment.Home3Fragment;
import com.qilu.pe.ui.fragment.Home4Fragment;
import com.qilu.pe.ui.fragment.Home5Fragment;
import com.ruitu.arad.Arad;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.base.BaseActivity;
import com.ruitu.arad.support.view.NoScrollViewPager;
import com.ruitu.arad.util.ScreenUtils;
import com.ruitu.arad.util.SizeUtils;
import com.ruitu.arad.util.ToastUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class MainActivity extends BaseActivity {

    private BottomNavigationViewEx bnve;
    private NoScrollViewPager vp;
    private int mPreTabIndex = 0;

    private VpAdapter adapter;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_main;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bnve = findViewById(R.id.bnve);
        vp = findViewById(R.id.vp);

//        vp.setOffscreenPageLimit(9);
        vp.setScanScroll(false);

        bnve.enableAnimation(false);
        bnve.enableShiftingMode(false);
        bnve.enableItemShiftingMode(false);

        bnve.setIconsMarginTop(SizeUtils.dp2px(10));
        bnve.setIconSize(23, 23);
        bnve.setTextSize(10);
        bnve.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                for (int i = 0; i < bnve.getMenu().size(); i++) {
                    MenuItem menu = bnve.getMenu().getItem(i);
                    switch (menu.getItemId()) {
                        case R.id.i_home:
                            menu.setIcon(R.mipmap.ic_home_1);
                            break;
                        case R.id.i_category:
                            menu.setIcon(R.mipmap.ic_home_2);
                            break;
                        case R.id.i_msg:
                            menu.setIcon(R.mipmap.ic_home_3);
                            break;
                        case R.id.i_cart:
                            menu.setIcon(R.mipmap.ic_home_4);
                            break;
                        case R.id.i_mine:
                            menu.setIcon(R.mipmap.ic_home_5);
                            break;
                    }
                }
                switch (item.getItemId()) {
                    case R.id.i_home:
                        item.setIcon(R.mipmap.ic_home_1_selected);
                        break;
                    case R.id.i_category:
                        item.setIcon(R.mipmap.ic_home_2_selected);
                        break;
                    case R.id.i_msg:
                        item.setIcon(R.mipmap.ic_home_3_selected);
                        break;
                    case R.id.i_cart:
                        item.setIcon(R.mipmap.ic_home_4_selected);
                        break;
                    case R.id.i_mine:
                        item.setIcon(R.mipmap.ic_home_5_selected);
                        break;
                }
                return true;
            }
        });
        MenuItem menu = bnve.getMenu().getItem(0);
        menu.setIcon(R.mipmap.ic_home_1_selected);

        adapter = new VpAdapter(getSupportFragmentManager());
        vp.setOffscreenPageLimit(5);
        vp.setAdapter(adapter);
        bnve.setupWithViewPager(vp);

        String mall_switch=Arad.preferences.getString(Config.MALL_SWITCH);
        if(!TextUtils.isEmpty(mall_switch)){
            int mallSwitch=Integer.valueOf(mall_switch);
            if(mallSwitch == 0){
                bnve.getBottomNavigationMenuView().removeViewAt(3);
                FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) bnve.getBottomNavigationMenuView().getLayoutParams();
                params.width = ScreenUtils.getScreenWidth() * 5 / 4;
                bnve.getBottomNavigationMenuView().setLayoutParams(params);
            }
        }


        vp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(final int position) {
                if (position == 0) {
                    adapter.getItem(0).onHiddenChanged(false);
                } else {
                    adapter.getItem(0).onHiddenChanged(true);
                }

//                if (position == 3 && MyApp.settings.getMall_switch() == 0) {
//                    final NormalDialog dialog = new NormalDialog(MainActivity.this);
//                    dialog.style(NormalDialog.STYLE_TWO);
//                    dialog.content("敬请期待");
//                    dialog.btnNum(1);
//                    dialog.btnText("确定");
//                    dialog.setCancelable(false);
//                    dialog.setCanceledOnTouchOutside(false);
//                    dialog.setOnBtnClickL(new OnBtnClickL() {
//                        @Override
//                        public void onBtnClick() {
//                            dialog.dismiss();
//                            vp.setCurrentItem(mPreTabIndex, false);
//                        }
//                    });
//                    dialog.show();
//                } else {
                    mPreTabIndex = position;
//                }
            }
        });

        Arad.bus.register(this);


    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetMsg(EventModel ev) {
        if (ev.getEventCode() == 20101651) {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (Arad.bus.isRegistered(this)) {
            Arad.bus.unregister(this);
        }
    }

    private class VpAdapter extends FragmentPagerAdapter {

        public VpAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 0) {// 首页
                return Home1Fragment.newInstance();
            } else if (position == 1) {// 患者
                return Home2Fragment.newInstance();
            } else if (position == 2) {// AI体检
                return Home3Fragment.newInstance();
            } else if (position == 3) {// 商城
                return Home4Fragment.newInstance();
            } else if (position == 4) {// 我的
                return Home5Fragment.newInstance();
            }

            return Home2Fragment.newInstance();
        }

        @Override
        public int getCount() {
            return 5;
        }
    }

    private long waitTime = 2000;
    private long touchTime = 0;

    @Override
    public void onBackPressed() {
        if (null != vp && vp.getCurrentItem() == 0) {
            long currentTime = System.currentTimeMillis();
            if ((currentTime - touchTime) >= waitTime) {
                ToastUtils.showShortSafe("再按一次退出程序");
                touchTime = currentTime;
            } else {
//                Intent backHome = new Intent(Intent.ACTION_MAIN);//启动一个意图,回到桌面
//                backHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                backHome.addCategory(Intent.CATEGORY_HOME);
//                startActivity(backHome);
                super.onBackPressed();
            }
        } else {
            vp.setCurrentItem(0);
        }
    }
}